//
//  AppDelegate.swift
//  weibo
//
//  Created by Felix on 16/11/3.
//  Copyright © 2016年 Felix. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        
        NotificationCenter.default.addObserver(self, selector: #selector(changeRootViewController(notice:)), name: NSNotification.Name.init(WBSwitchRootViewController), object: nil)
        
        window = UIWindow.init(frame: UIScreen.main.bounds);
        window?.backgroundColor = UIColor.white
        window?.rootViewController = defaultViewController()
        window?.makeKeyAndVisible()
        
        UINavigationBar.appearance().tintColor = UIColor.orange
        UITabBar.appearance().tintColor = UIColor.orange
        
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
    }

    func applicationWillTerminate(_ application: UIApplication) {
    }


}


extension AppDelegate{

    
    //切换根控制器
    func changeRootViewController(notice:NSNotification) {
        //是否进入主界面
        if notice.object as! Bool {
            window?.rootViewController = WBMainViewController()
        }else{
            let sb = UIStoryboard.init(name: "WBWelCome", bundle: nil)
            window?.rootViewController = sb.instantiateInitialViewController()!
        }
    }
    
    //返回一个默认界面
    func defaultViewController() -> UIViewController? {
        if WBUserAccount.isLoggin() {
            if isNewVersion() {
                let sb = UIStoryboard.init(name: "WBNewFeature", bundle: nil)
                guard let VC = sb.instantiateInitialViewController() else {
                    return nil
                }
                return VC
            }else{
                let  sb = UIStoryboard.init(name: "WBWelCome", bundle: nil)
                guard let VC = sb.instantiateInitialViewController() else {
                    return nil
                }
                return VC
                
            }
            
        }else{
            
            return WBMainViewController()
            
        }
    }
    
    
    //判断是否有新版本
    private func isNewVersion() -> Bool{
        let currentVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as!String
        
        let defaults = UserDefaults.standard
        let sanboxVersion = (defaults.value(forKey: "ooxx") as? String) ?? "0.0"
        
        if currentVersion.compare(sanboxVersion) == ComparisonResult.orderedDescending {
            defaults.set(currentVersion, forKey: "ooxx")
            return true
        }
        return false
    }
    
    
    
}



func AdaLog<T>(message:T , fileName:String = #file, methedName:String = #function , line:Int = #line ){
    #if DEBUG
    print("\((fileName as NSString).pathComponents.last!)\n \(methedName)\n\(line)\n\(message)\n")
    #endif
}

